home *** CD-ROM | disk | FTP | other *** search
- Path: news.ti.com!usenet
- From: srikumar@india.ti.com (Srikumar K.P)
- Newsgroups: comp.lang.c++
- Subject: Re: Arguments with streams
- Date: 01 Jan 1996 22:33:44 +0500
- Organization: Texas Instruments (India)
- Sender: srikumar@mmplsparc1.india.ti.com
- Message-ID: <5o68evn7k7.fsf@mmplsparc1.india.ti.com>
- References: <4c85u1$cga@ralph.vnet.net>
- NNTP-Posting-Host: mmplsparc1.india.ti.com
- In-reply-to: jason@char.vnet.net's message of 1 Jan 1996 08:27:44 GMT
- X-Newsreader: (ding) Gnus v0.99.27
-
-
- jason@char.vnet.net wrote:
- >I've got a small problem. I'm working on a C++ library for HTML forms
- >and wanted to add the some functionality to 'cout' (ostream), but
- >am stumped.
-
- >Here's what I want:
-
- > cout << A("http://www.vnet.net") << "Vnet" << endA << endl;
-
- >ostream& endA (ostream& f)
- > { f << "</a>"; return f; };
- >ostream& A(ostream& f, char *s)
- > { f << "<a " << s << ">"; return f; }
-
- >My problem lies with 'A'. Is there any way to say that the first argument
- >to the function is the ostream at the beginning of the statement without
- >explicitly adding it (ie. cout << A(cout, "htt...") << ... << endl; works).
-
- >I've looked briefly at the way setw is implemented both in g++ and MSVC,
- >but the code looks over my head =)
-
- The method for doing this is the IOSTREAM manipulators.
- Using manipulators it is possible to insert a vareity
- of objects into the stream along with any other types.
-
- declare a class like this :
-
- class A { };
-
- ostream& operator<< (ostream &os, A a)
- {
- // do whatever u want on os using A
- return os
- }
-
- Now u can use a code like this ->
-
- int y;
-
- cout << A("any") << y << "\n";
-
- Go thru IoManipulators as specified in C++ Programming
- language Book by Stroustrop to get a better idea ..
-
- Regards srikumar
-
-
-